import threading


class VolumeMeter:
  def __init__(self, buf, callback):
    self.buf = buf #samples buffer
    self.callback = callback #function that knows what to do with volume
    self.work = False
    
  def loopNode (self):
    if(self.work):
      threading.Timer(0.25, self.loopNode).start ()
    data = self.buf.read(100)
    if(data):
      sum = 0
      for x in data:
        sum += x
      print (sum/len(data))

  def start(self):
    self.work = True
    self.loopNode()

  def stop(self):
    self.work = False
